$(function () {
      class Login {
            constructor() {

                  this.username = $("#username");
                  this.password = $("#password");
                  this.login_btn = $("#loginbtn");
                  // 提示框 ; 
                  this.alert = $("#register-alert");
                    
                 
                  this.bindEvent();
                  this.usermsg = JSON.parse(sessionStorage.getItem("usermsg"));
                  if (this.usermsg !== null) {
                        this.writeUserMsg();
                  }
            }
            writeUserMsg() {
                  this.username.val([this.usermsg.username])
                  this.password.val([this.usermsg.password])
            }
            bindEvent() {
                  $("#loginbtn").click(() => {
                        let res = this.login();
                  })
            }
            login() {
                  $.ajax({
                        url: "http://127.0.0.1:8888/users/login",
                        data: {
                              username: this.username.val(),
                              password: this.password.val()
                        },
                        type: "POST",
                        success: function (res) {
                              return res
                        }
                  })
                  .then(res => {
                        switch (res.code) {
                              case 0:
                                    this.error(res);
                                    break;
                              case 1:
                                    this.success();
                                    break;
                        }
                  })

            }
            // formatData(data) {
            //       var _data = "";
            //       for (var key in data) {
            //             _data += "&" + key + "=" + data[key];
            //       }
            //       _data = _data.slice(1);

            //       return _data;
            // }

            error(res) {
                  this.alert.removeClass("alert-success", "alert-danger");
                  this.alert.addClass("alert-danger");
                  this.alert.html("抱歉登录失败 : " + res.message)

                  setTimeout(() => {
                        this.alert.removeClass("alert-success", "alert-danger");
                  }, 2000)
            }
            success() {
                  this.alert.removeClass("alert-success", "alert-danger");
                  this.alert.addClass("alert-success");
                  this.alert.html("恭喜登录成功稍后我们将为您跳转首页");
                  setTimeout(() => {
                        location.href = "./index.html";
                        sessionStorage.setItem("usermsg", JSON.stringify({
                              username: this.username.val(),
                              password: this.password.val()
                        }));
                  }, 2000)
            }
      }

      let l = new Login;

});